// PROGRAMA : TABPC001
// BIBLIOTECA : PRODETR
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO, DETRAN E SEFAZ
// PROGRAMADOR : EDU/FM DATA: 27/05/92
// OBJETIVO : CONSULTA TABELA DE APOIO OPCAO: 'CTC' E 'CTD'
// ATUALIZACOES DATA
// PROGRAMA : TABPC001 05/11/1997
// BIBLIOTECA : SISP
// PROGRAMADOR :
// OBJETIVO : ADAPTACAO PARA O SHOPPING DO CIDADAO (P200)
// : ALTERACAO DE TELA 30/09/1999
// OBS : ESTE PROGRAMA FOI REESTRUTURADO POR SOLON MIRANDA
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Tabpc001A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Tabtc001;
import br.com.vti.sisp_sicrim.antigo.model.SicTabelas;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Tabpc001 OBJETIVO : CONSULTA TABELA DE APOIO OPCAO: 'CTC' E 'CTD' *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/tabpc001")
public class Tabpc001 {
	// Views utilizadas
	SicTabelas vwDvetab = new SicTabelas();
	SicTabelas vwTabele = new SicTabelas();
	// Telas/relatorios utilizados
	Tabpc001A tabpc001A;
	Tabtc001 tabtc001;
	// Includes utilizados
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Tabpc001 tabpc001Anterior;
	String faseproc = "0";

	private int m = 0;
	private int j = 0;

	private int i = 0;
	private String[] ele = new String[13];
	private String tabdec = "";
	private String cod4 = "";
	private String tabcha = "";
	private String jDescricao = "";
	private String jRia = "";
	private boolean achou;

	private Tabpc001A preMapas() {
		int ind = 0;
		tabpc001A = new Tabpc001A(true, sessao);
		tabtc001 = new Tabtc001(true, sessao);

		Arrays.fill(ele, "");
		tabpc001A.setDataCorrente(UtilBean.obtemDataDia());
		tabpc001A.setNomeClasse(this.getClass().getSimpleName().toUpperCase());
		tabtc001.setDataCorrente(UtilBean.obtemDataDia());
		tabtc001.setNomeClasse(this.getClass().getSimpleName().toUpperCase());
		// Par�grafo: r0()
		return tabpc001A;
	}

	public Tabpc001(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Tabpc001A tabpc001A, Tabtc001 tabtc001) {
		try {
			main(tabpc001A, tabtc001);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroSiscerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Tabpc001A ptabpc001A, Tabtc001 ptabtc001) throws Exception {
		if (ptabpc001A == null && ptabtc001 == null) {
			primeiraVez = true;
			tabpc001A = preMapas();
			tabpc001A.setNavegar("../tabpc001/executar");
			result.include("tabpc001A", tabpc001A);
			sessao.setAtributo("TABPC001", this);
			tabpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(ptabpc001A, ptabtc001);
		}

		// tabtc001.setConf("S");

		if (ptabtc001 != null) {

			if (ptabtc001.getConf().trim().equals("N")) {
				// tabpc001A = preMapas();
				tabpc001A.setMensagem("");
				ptabtc001.setConf("S");
				tabpc001A.setNavegar("../tabpc001/executar");
				result.include("tabpc001A", tabpc001A);
				sessao.setAtributo("TABPC001", this);
				tabpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
				return;
			}

			if (ptabtc001.getFuncao().equals(UtilBean.ctt_retorna)) {
				tabpc001A.setMensagem("");
				tabpc001A.enviarMensagem(null, "");
				result.include("tabpc001A", tabpc001A);
				sessao.setAtributo("TABPC001", this);
				tabpc001A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (ptabtc001.getCodtab() > 0) {
				tabpc001A.setCodtab(ptabtc001.getCodtab());
				tabpc001A.setDescri(ptabtc001.getDescri());
				vwDvetab.fecha();
				;
			}

		}

		if (ptabpc001A != null) {

			if (ptabpc001A.getFuncao().equals(UtilBean.ctt_retorna)) {
				Sispm001 sispm001 = new Sispm001(result, sessao);
				sispm001.executar(null, null, null);
				return;
			}

		}

		if (ptabpc001A != null) {
			tabpc001A.receberTela(false, result);
			if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
				tabpc001A.enviarMensagem(null, "");
				result.include("tabpc001A", tabpc001A);
				sessao.setAtributo("TABPC001", this);
				tabpc001A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
		}
		switch (tabpc001A.getOperacao().trim()) {
		case "CTC": {
			monta();
			vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 0, 3, "N", tabpc001A.getCodtab());
			if (tabpc001A.getCodtab() == 001) {

				/*
				 * if (tabpc001A.getUf().isEmpty()) { result.include("tabpc001A", tabpc001A); sessao.setAtributo("TABPC001", this); tabpc001A.enviarTelaEstado("uf", result,
				 * getClass().getSimpleName().toUpperCase()); return; }
				 */

				tabcha = UtilBean.moveSubstring(tabcha, 0, 3, "N", 001);
				// Par�grafo: rep3()

				// tabpc001A.setCodtab(0);
				// Par�grafo: rd3()

				if (tabtc001.getConf().equals("N")) {
					break;
				}
				zerar();
				while (vwDvetab.leituraTabchaMaiorig(tabcha, tabpc001A.getCodtab())) {
					// READADA
					if (!tabpc001A.getUf().trim().equals("")) {
						if (!vwDvetab.getChaveAlfaAcesso().trim().equals(tabpc001A.getUf())) {
							continue;
						}
					}

					moveDvetab(ptabtc001);
					if (vwDvetab.fimDados() || i == 14) {
						if (i > 0) {
							telao(ptabtc001);
						}
						if (vwDvetab.fimDados()) {
							tabtc001.setConf("N");
						}
						break;
					}
					if (vwDvetab.quebraDiatabchalfa()) {
						if (!tabpc001A.getUf().trim().equals("") && i > 0) {
							telao(ptabtc001);
							break;
						}
					}

				}

			} else {

				if (tabtc001.getConf().equals("N")) {
					break;
				}
				/*
				 * if (tabtc001.getCodtab() > 0) { break; }
				 */

				if (tabpc001A.getCodtab() == 18) {

					/*
					 * if (!tabpc001A.getEnviouOpcao()) { result.include("tabpc001A", tabpc001A); sessao.setAtributo("TABPC001", this); tabpc001A.enviarTelaOpcao("jOpcao", result,
					 * getClass().getSimpleName().toUpperCase()); return; }
					 */
					if (!(tabpc001A.getJopcao().equals("A") || tabpc001A.getJopcao().equals("E") || tabpc001A.getJopcao().trim().equals(""))) {
						tabpc001A.enviarMensagem(null, "");
						result.include("tabpc001A", tabpc001A);
						sessao.setAtributo("TABPC001", this);
						tabpc001A.enviarTelaOpcao("jOpcao", result, getClass().getSimpleName().toUpperCase());
						return;
					}
					tabcha = UtilBean.moveSubstring(tabcha, 0, 3, "N", 18);
					// Par�grafo: rep4()

					tabpc001A.setCodtab(0);
					// Par�grafo: rd4()
					Long codelem;
					if (StringUtils.isNumeric(tabpc001A.getDescri())) {
						codelem = Long.valueOf(tabpc001A.getDescri());
					} else {
						codelem = 0L;
					}
					zerar();
					while (vwDvetab.leituraTabeleMaiorig(18, codelem)) {
						if (!tabpc001A.getJopcao().trim().equals("")) {
							jDescricao = vwDvetab.getDescricaoElem();
							jDescricao = StringUtils.rightPad(jDescricao, 60, " ");
							jRia = jDescricao.charAt(31) + "";
							if (tabpc001A.getJopcao().equals("E")) {
								if (!(tabpc001A.getJopcao().charAt(0) == jRia.charAt(0))) {
									continue;
								}
							} else {
								if (jRia.equals("E")) {
									continue;
								}
							}
						}
						if (vwDvetab.getCodigoTabela() != 18) {
							break;
						}

						if (!tabpc001A.getUf().trim().equals("")) {
							if (!vwDvetab.getChaveAlfaAcesso().trim().equals(tabpc001A.getUf())) {
								continue;
							}
						}

						moveDvetab(ptabtc001);
						if (vwDvetab.fimDados() || i == 14) {
							if (i > 0) {
								telao(ptabtc001);
							}
							if (vwDvetab.fimDados()) {
								tabtc001.setConf("N");
							}
							break;
						}

					}

				} else {
					// Par�grafo: rep1()

					j = 12;
					for (i = 12; i >= 1; i += -1) {
						if (UtilBean.validarMascara(ele[i], "N")) {

							cod4 = UtilBean.moveSubstring(cod4, j, 1, "A", ele[i]);
							j = j;
						}
						vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 0, 3, "N", tabtc001.getCodtab());
						vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 3, 12, "N", cod4);
						tabpc001A.setCodtab(0);
						// Par�grafo: rd1()
						Long codelem;
						if (StringUtils.isNumeric(tabpc001A.getDescri())) {
							codelem = Long.valueOf(tabpc001A.getDescri());
						} else {
							codelem = 0L;
						}
						zerar();
						while (vwDvetab.leituraTabeleMaiorig(tabpc001A.getCodtab(), codelem)) {

							if (!tabpc001A.getUf().trim().equals("")) {
								if (!vwDvetab.getChaveAlfaAcesso().trim().equals(tabpc001A.getUf())) {
									continue;
								}
							}

							moveDvetab(ptabtc001);
							if (vwDvetab.fimDados() || i == 14) {
								if (i > 0) {
									telao(ptabtc001);
								}
								if (vwDvetab.fimDados()) {
									tabtc001.setConf("N");
								}
								break;
							}

						}

					}
				}
				break;
			}
		}
		case "CTD": {

			if (tabtc001.getConf().equals("N")) {
				break;
			}
			/*
			 * if (tabtc001.getCodtab() > 0) { break; }
			 */

			if (tabpc001A.getCodtab() == 18) {

				/*
				 * if (!tabpc001A.getEnviouOpcao()) { result.include("tabpc001A", tabpc001A); sessao.setAtributo("TABPC001", this); tabpc001A.enviarTelaOpcao("jOpcao", result,
				 * getClass().getSimpleName().toUpperCase()); return; }
				 */

				if (!(tabpc001A.getJopcao().equals("A") || tabpc001A.getJopcao().equals("E") || tabpc001A.getJopcao().trim().equals(""))) {
					tabpc001A.enviarMensagem(null, "");
					tabpc001A.setNavegar("../tabpc001/executar");
					result.include("tabpc001A", tabpc001A);
					sessao.setAtributo("TABPC001", this);
					if (!result.used())
						tabpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
					return;
				}
				// Par�grafo: rep2()

				tabdec = UtilBean.moveSubstring(tabdec, 0, 3, "N", tabpc001A.getCodtab());
				tabdec = UtilBean.moveSubstring(tabdec, 3, 60, "A", tabpc001A.getDescri());
				// tabtc001.setCodtab(0);
				// Par�grafo: rd2()
				zerar();
				while (vwDvetab.leituraTabdecMaiorig(tabpc001A.getCodtab(), tabpc001A.getDescri())) {
					if (!tabpc001A.getJopcao().trim().equals("")) {

						jDescricao = vwDvetab.getDescricaoElem();
						jDescricao = StringUtils.rightPad(jDescricao, 60, " ");
						jRia = jDescricao.charAt(31) + "";
						if (tabpc001A.getJopcao().equals("E")) {
							if (!(tabpc001A.getJopcao().charAt(0) == jRia.charAt(0))) {
								continue;
							}
						} else {
							if (jRia.equals("E")) {
								continue;
							}
						}
					}
					if (vwDvetab.getCodigoTabela() != 18) {
						continue;
					}

					if (!tabpc001A.getUf().trim().equals("")) {
						if (!vwDvetab.getChaveAlfaAcesso().trim().equals(tabpc001A.getUf())) {
							continue;
						}
					}

					moveDvetab(ptabtc001);
					if (vwDvetab.fimDados() || i == 14) {
						if (i > 0) {
							telao(ptabtc001);
						}
						if (vwDvetab.fimDados()) {
							tabtc001.setConf("N");
						}
						break;
					}

				}

			} else {
				// Par�grafo: rep5()

				tabdec = UtilBean.moveSubstring(tabdec, 0, 3, "N", tabpc001A.getCodtab());
				tabdec = UtilBean.moveSubstring(tabdec, 3, 60, "A", tabpc001A.getDescri());
				// tabtc001.setCodtab(0);
				// Par�grafo: rd5()
				zerar();
				while (vwDvetab.leituraTabdecMaiorig(tabpc001A.getCodtab(), tabpc001A.getDescri())) {

					if (!tabpc001A.getUf().trim().equals("")) {
						if (!vwDvetab.getChaveAlfaAcesso().trim().equals(tabpc001A.getUf())) {
							continue;
						}
					}

					moveDvetab(ptabtc001);
					if (vwDvetab.fimDados() || i == 14) {
						if (i > 0) {
							telao(ptabtc001);

						}
						if (vwDvetab.fimDados()) {
							tabtc001.setConf("N");
						}
						break;
					}

				}

				if (tabtc001.getConf().equals("N")) {
					break;
				}
				/*
				 * if (tabtc001.getCodtab() > 0) { break; }
				 */

			}
			break;
		}
		default: {
			decideOperacao();
		}
		}
		tabtc001.setNavegar("../tabpc001/executar");
		sessao.setAtributo("TABPC001", this);
		result.include("tabtc001", tabtc001);
		if (!result.used() && achou) {
			tabtc001.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
		} else {
			if (ptabtc001 == null) {
				tabpc001A.setMensagem("N�O ENCONTROU REGISTROS COM AS INFORMA��ES");
			}

			tabpc001A.setNavegar("../tabpc001/executar");
			result.include("tabpc001A", tabpc001A);
			sessao.setAtributo("TABPC001", this);
			if (!result.used())
				tabpc001A.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
		}
	}

	private void moveDvetab(Tabtc001 ptabtc001) {
		i += 1;
		if (vwDvetab.getCodigoTabela() != 18) {

			tabtc001.setCha0(i, vwDvetab.getChaveAlfaAcesso());
		}
		achou = true;
		tabtc001.setObs0(i, vwDvetab.getObservacao());
		tabtc001.setGru0(i, vwDvetab.getGrupoElem());
		tabtc001.setDat0(i, vwDvetab.getDataAtualizacao());
		tabtc001.setMat0(i, vwDvetab.getMatriculaFunc());
		tabtc001.setTab0(i, vwDvetab.getCodigoTabela());
		tabtc001.setCod0(i, vwDvetab.getCodigoElem());
		tabtc001.setDes0(i, vwDvetab.getDescricaoElem());
		/*
		 * if (i == 14) { telao(ptabtc001); }
		 */
	}

	private void telao(Tabtc001 ptabtc001) {
		if (!(tabtc001.getOperacao().equals("CTC") || tabtc001.getOperacao().equals("CTD"))) {
			funcoes.sissm001(result, sessao);
		}
		// PERFORM ROT01-WYZ-K7

		i = 0;
		if (ptabtc001 == null) {
			tabtc001.setNavegar("../tabpc001/executar");
			result.include("tabtc001", tabtc001);
			sessao.setAtributo("TABPC001", this);
			tabtc001.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			// tabtc001.receberTela(false, result);
			tabtc001.setNavegar("../tabpc001/executar");
			result.include("tabtc001", tabtc001);
			sessao.setAtributo("TABPC001", this);
			tabtc001.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;

		}

	}

	private void zerar() {
		for (int i1 = 1; i1 <= 14; i1++) {
			tabtc001.setCha0(i1, "");
			tabtc001.setCod0(i1, 0);
			tabtc001.setDes0(i1, "");
			tabtc001.setTab0(i1, 0);
			tabtc001.setDat0(i1, 0);
			tabtc001.setMat0(i1, 0);
		}
	}

	private void monta() throws Exception {
		m = 12;
		j = 12;
		do {
			if (UtilBean.validarMascara(ele[m], "N")) {

				cod4 = UtilBean.moveSubstring(cod4, j, 1, "A", ele[m]);
				j = j - 1;
			}

			m = m - 1;
		} while (!(m == 0));
		vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 3, 12, "N", cod4);
	}

	private void decideOperacao() {
		// CHAMA PROGRAMA DE ACORDO COM OPERACAO
		funcoes.sissm001(result, sessao);
	}

	private void restaurarEstados(Tabpc001A ptabpc001A, Tabtc001 ptabtc001) {
		this.tabpc001Anterior = (Tabpc001) sessao.getAtributo("TABPC001");
		if (ptabpc001A != null) {
			tabpc001A = ptabpc001A;
		} else {
			tabpc001A = this.tabpc001Anterior.tabpc001A;
		}
		tabpc001A.setSessao(sessao);
		if (ptabtc001 != null) {
			tabtc001 = ptabtc001;
			this.vwDvetab = this.tabpc001Anterior.vwDvetab;
		} else {
			tabtc001 = this.tabpc001Anterior.tabtc001;
		}
		tabtc001.setSessao(sessao);
		this.faseproc = this.tabpc001Anterior.faseproc;

	}
}
